<?php
/**
 * @title 684. 冗余连接 - 迭代
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $edges
     * @return Integer[]
     */
    function findRedundantConnection($edges) {
        /**
         * @since 2021-12-03 节点数量
         */
        $len = count($edges);

        /**
         * @since 2021-12-03 线段关系
         */
        $lineArray = [];
        foreach ($edges as list($s, $e)){
            $lineArray[$s][$e] = 1;
            $lineArray[$e][$s] = 1;
        }
        // return $lineArray;

        /**
         * @since 2021-12-03 依次尝试
         */
        for($i=count($edges)-1; $i>=0; $i--){
            $lines = $lineArray;
            list($s, $e) = $edges[$i];

            /**
             * @since 2021-12-03 删除
             */
            unset($lines[$s][$e]);
            unset($lines[$e][$s]);

            /**
             * @since 2021-12-08 s作为起点遍历
             */
            $nodes = $hashNodeArray = [$s=>1];
            while($nodes){
                $nextNodes = [];

                foreach ($nodes as $node=>$val){
                    foreach ($lines[$node] as $k=>$v){
                        unset($lines[$k][$node]);

                        if(!isset($hashNodeArray[$k])){
                            $hashNodeArray[$k] = 1;
                            $nextNodes[$k] = 1;
                        } else {}
                    }

                    unset($lines[$node]);
                }

                /**
                 * @since 2021-12-08 下次迭代
                 */
                $nodes = $nextNodes;
            }

            /**
             * @since 2021-12-08 是否符合条件
             */
            if(count($hashNodeArray) == $len){
                return $edges[$i];
                break;
            } else {}
        }

        /**
         * @return
         */
        return [];
    }
}

/**
 * @url http://127.0.0.1/leetcode/202112/2021.12.03_2.php
 */
$datas = [
    [[[1,2], [1,3], [2,3]]], // [2,3]
    [[[1,2], [2,3], [3,4], [1,4], [1,5]]], // [1,4]
    [[[1,4],[3,4],[1,3],[1,2],[4,5]]], // [1,3]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->findRedundantConnection(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
